﻿using System;
using System.Collections;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Audience;
using Microsoft.Office.Server.WebControls;
using Microsoft.SharePoint;

namespace SKN.SPMOSS
{
    public static class Audiences
    {
        const string c_OOTBAudienceColumn = "Audience";

        public static void CurrentUserInAudience (SPSite site, SPList list)
        {

            ServerContext context = ServerContext.GetContext(site);
            AudienceManager audManager = new AudienceManager(context);
            //your code here


            AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();

            foreach (SPListItem listItem in list.Items)
            {
                // get roles the list item is targeted to
                string audienceFieldValue = (string)listItem[c_OOTBAudienceColumn];

                // quickly check if the user belongs to any of those roles
                if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue,
                                     false))
                {
                    // is a member
                }
                else
                {
                    // not a member
                }
            }


        }

        public static bool CurrentUserInAudience (SPSite site, SPList list, string audName)
        {
            ServerContext context = ServerContext.GetContext(site);
            AudienceManager audManager = new AudienceManager(context);

            //your code here
            AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
            foreach (SPListItem listItem in list.Items)
            {
                // get roles the list item is targeted to
                string audienceFieldValue = (string)listItem[c_OOTBAudienceColumn];

                // quickly check if the user belongs to any of those roles
                if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, false))
                {
                    return true;
                }
            }
            return false;
        }

        public static Audience CreateNewAudience (SPSite site, string sAudName, string sDescription)
        {
            ServerContext context = ServerContext.GetContext(site);
            AudienceManager audManager = new AudienceManager(context);

            AudienceCollection ac = audManager.Audiences;
            Audience newAudience = null;

            try
            {
                newAudience = ac.Create(sAudName, sDescription);

            }
            catch (AudienceDuplicateNameException e)
            {
                //Your exception handling code here
            }
            catch (AudienceException e1)
            {
                //Your exception handling code here
            }
            return newAudience;
        }

        public static void AddAudienceFilter (SPSite site, string sAudName)
        {
            ServerContext context = ServerContext.GetContext(site);

            AudienceManager AudMgr = new AudienceManager(context);
            Audience a = null;
            bool ruleListNotEmpty = false;
            try
            {
                a = AudMgr.Audiences[sAudName];
            }
            catch (AudienceArgumentException ex)
            {
                //your exception handling code here
            }

            ArrayList aRules = a.AudienceRules;
            if (aRules == null)
            {
                aRules = new ArrayList();
            }
            else
            {
                ruleListNotEmpty = true;
            }

            try
            {
                //if the rule is not emply, start with a group operator 'AND' to append
                if (ruleListNotEmpty)
                {
                    aRules.Add(new AudienceRuleComponent(null, "AND", null));
                }

                AudienceRuleComponent r0 = new AudienceRuleComponent(null, "(", null);
                aRules.Add(r0);

                AudienceRuleComponent r1 = new AudienceRuleComponent("Department", "Contains", "HR");
                aRules.Add(r1);               

                AudienceRuleComponent r2 = new AudienceRuleComponent(null, ")", null);
                aRules.Add(r2);

                a.AudienceRules = aRules;

                a.Commit();
            }
            catch (AudienceException e)
            {
                //Your exception handling code here
            }
        }

        public static Guid GetAudienceId (SPSite site, string audienceName)
        {
            ServerContext context = ServerContext.GetContext(site);
            AudienceManager audManager = new AudienceManager(context);
            AudienceCollection audCollection = audManager.Audiences;
            
            foreach (Audience aud in audCollection)
            {
                if (aud.AudienceName == audienceName)
                    return aud.AudienceID;
            }
            return Guid.Empty;
        }
    }
}
